home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / ms_dos / iplink / iplinkf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-08  |  12.5 KB  |  539 lines

  1. /*    PC98<->IBM<->FMR ファイル転送 for FMR Copyright (c) E.Suto  , 1992-1993    */
  2. /*    修正履歴  Ver  0.00     1992/06/22     試作品                                    */
  3. /*              Ver  0.10     1992/06/23     近所公開版                                */
  4. /*              Ver  0.20     1992/06/26     簡易サーバーモード追加,共用ドライブ対応*/
  5. /*              Ver  0.21     1992/06/28     リモートコマンド追加                    */
  6. /*              Ver  0.30     1992/07/01     TIMEOUT()改造,オプション統合,速度表示    */
  7. /*              Ver  0.31     1992/07/03     簡易サーバーを出来るだけ止めないよう    */
  8. /*              Ver  0.32     1992/07/03     速度表示の0割り算ガード(超手抜き)    */
  9. /*              Ver  0.33     1992/07/06     . の消し方変更                            */
  10. /*              Ver  0.34     1992/07/10     受信ファイルがルートに行ってしまうバグ修正    */
  11. /*              Ver  0.35     1992/11/17     RS232Cのバグ修正(98版のみ)    */
  12. /*              Ver  0.36     1992/11/17     ちょっと高速化(^^;                        */
  13. /*              Ver  0.37     1992/11/18     タイマ値最適化                            */
  14. /*              Ver  0.38     1992/11/18     送信ファイル名バグ修正                    */
  15. /*              Ver  0.39     1992/11/18     サーバーバグ修正                        */
  16. /*              Ver  0.40     1993/02/04     FMR版作成                            */
  17. /*              Ver  0.41     1993/02/04     inpのwaitを#defineに                    */
  18. /*              Ver  0.42     1993/02/04     ソースをちょっと統合(^^;                */
  19. /*              Ver  1.00     1993/ 2/ 3     失敗時にタイムアウトしない                */
  20. /*              Ver  1.01     1993/ 2/ 4     コマンド受信処理でタイムアウトしない    */
  21.  
  22. #include "iplink.h"
  23.  
  24. #define RS_IN        0x0a04
  25. #define RS_OUT        0x0a02
  26. #define PALAWAIT    0l    /* TIMEOUTが発生する場合はこの値を大きくしてね(^^; */
  27.  
  28. void main(int argc,char *argv[])
  29. {
  30.     int i , k , l , m ;
  31.     unsigned char *c ;
  32.  
  33.     debug    = 0 ;
  34.     mode    = -1 ;
  35.     strcpy( command , "" ) ;
  36.     strcpy( path , "" ) ;
  37.  
  38. /* コピーライト表示 */
  39.     copyright() ;
  40.  
  41. /* 割り込み処理定義 */
  42.     signal( SIGINT , sig_out ) ;
  43.  
  44. /* オプションチェック */
  45.     if( argc < 2 ) {
  46.         usage() ;
  47.         exit( 0 ) ;
  48.     }
  49.     else {
  50.         k = l = 0 ;
  51.         for( i=1 ; i<argc ; i++ ) {
  52.             c = argv[i] ;
  53.             if( *c == '-' || *c == '/' ) {
  54.                 switch( *++c ) {
  55.                 case '?' :
  56.                     usage();
  57.                     exit( 0 );
  58.                 case 'i' :
  59.                 case 'I' :
  60.                     debug = -1 ;
  61.                     break ;
  62.                 case 'R' :
  63.                 case 'r' :
  64.                     if( k ) mode = 4 ;
  65.                     else mode = 1 ;
  66.                     break ;
  67.                 case 'S' :
  68.                 case 's' :
  69.                     mode = 0 ;
  70.                     break ;
  71.                 case 'Z' :
  72.                 case 'z' :
  73.                     mode = 2 ;
  74.                     break ;
  75.                 case 'C' :
  76.                 case 'c' :
  77.                     mode = 5 ;
  78.                     c++ ;
  79.                     strcpy( command , c ) ;
  80.                     if( command[0] == '"' ) {
  81.                         for( ; i < argc ; ) {
  82.                             i++ ;
  83.                             strcat( command , " " ) ;
  84.                             strcat( command , argv[i] ) ;
  85.                             if( command[strlen( command ) - 1 ] == '"' ) 
  86.                                 break ;
  87.                         }
  88.                     }
  89.                     break ;
  90.                 case 'X' :
  91.                 case 'x' :
  92.                     c++ ;
  93.                     strcpy( path , c ) ;
  94.                     l = -1 ;
  95.                     break ;
  96.                 default :
  97. perr:                    printf( "ファイル名が2つ以上指定されているか、誤ったパラメタ(%s)が指定されています。\n" , argv[i] ) ;
  98.                     usage() ;
  99.                     exit( -1 ) ;
  100.                 }
  101.             }
  102.             else {
  103.                 if( mode == 5 ) {
  104.                     if( strlen( command ) > 0 ) goto perr ;
  105.                     strcpy( command , c ) ;
  106.                     if( command[0] == '"' ) {
  107.                         for( ; i < argc ; ) {
  108.                             i++ ;
  109.                             strcat( command , " " ) ;
  110.                             strcat( command , argv[i] ) ;
  111.                             if( command[strlen( command ) - 1 ] == '"' ) 
  112.                                 break ;
  113.                         }
  114.                     }
  115.                     else {
  116.                         for( ; i < argc ; ) {
  117.                             i++ ;
  118.                             if( ( *(argv[i]) == '-' ) || ( *(argv[i]) == '/' ) ) 
  119.                                 break ;
  120.                             strcat( command , " " ) ;
  121.                             strcat( command , argv[i] ) ;
  122.                         }
  123.                     }
  124.                 }
  125.                 else {
  126.                     if( k ) goto perr ;
  127.                     strcpy( name , argv[i] ) ;
  128.                     k = -1 ;
  129.                     if( mode == 1 ) mode = 4 ;
  130.                 }
  131.             }
  132.         }
  133.     }
  134.  
  135. /* オプションの関連チェック */
  136.     if( mode == -1 ) {
  137.         printf( "-r,-s,-z又は-c,の何れかを指定してください。\n" ) ;
  138.         usage() ;
  139.         exit( -1 ) ;
  140.     }
  141.     if( !( k ) && ( mode == 0 ) ) {
  142.         printf( "-s指定時にはファイル名を省略できません。\n" ) ;
  143.         usage() ;
  144.         exit( -1 ) ;
  145.     }
  146.     if( l && ( mode != 0 ) ) {
  147.         printf( "-s 指定時以外にはでは、-xは指定できません。\n" ) ;
  148.         usage() ;
  149.         exit( -1 ) ;
  150.     }
  151.     if( mode == 5 ) {
  152.         strcpy( buffer , command ) ;
  153.         if( buffer[0] == '"' ) {
  154.             m = strlen( buffer ) ;
  155.             if( buffer[m-1] != '"' ) {
  156.                 printf( " コマンドの終わりを示す\"が不足しています。(%s)\n",buffer ) ;
  157.                 usage() ;
  158.                 exit( -1 ) ;
  159.             }
  160.             buffer[m-1] = 0x00 ;
  161.             strcpy( command , ( buffer + 1 ) ) ;
  162.         }
  163.         if( strlen( command ) == 0 ) {
  164.             printf( "-c に続いてリモートコマンドを指定してください。\n" ) ;
  165.             usage() ;
  166.             exit( -1 ) ;
  167.         }
  168.         if( strlen( command ) > 114 ) {
  169.             printf( "申し訳有りませんがコマンドが長すぎます。114文字以下にしてください。\n" ) ;
  170.             usage() ;
  171.             exit( -1 ) ;
  172.         }
  173.     }
  174.  
  175. /* タイムアウト値設定 */
  176.     timeset() ;
  177.  
  178. /* 回線の初期化 */
  179.     rs_init() ;
  180.  
  181. /* 送受信処理へ */
  182.     switch( mode ) {
  183.         case 0 :                /* 送信処理 */
  184.             connect_s() ;
  185.             fp_send() ;
  186.             break ;
  187.         case 1 :                /* 受信処理 */
  188.             connect_r() ;
  189.             fp_receive0() ;
  190.             fp_receive() ;
  191.             break ;
  192.         case 2 :                /* サーバー処理 */
  193.             fp_server() ;
  194.             break ;
  195.         case 4 :                /* サーバーからの受信処理 */
  196.             connect_s() ;
  197.             fp_file() ;
  198.             fp_receive0() ;
  199.             fp_receive() ;
  200.             break ;
  201.         case 5 :                /* リモートコマンド処理 */
  202.             connect_s() ;
  203.             fp_command() ;
  204.             break ;
  205.     }
  206.  
  207. /* 後始末 */
  208.     printf( "電送処理終了しました。\n" ) ;
  209.     rs_init() ;
  210.  
  211. }
  212.  
  213. /* 接続確認待ち(送信型)*/
  214. int connect_s()
  215. {
  216.     unsigned char stat ;
  217.  
  218. /* 接続確認 */
  219.     if( mode != 2 ) printf( "接続待ちです。\n" ) ;
  220.     outp( RS_OUT , 0x02 ) ;        /* ずーと待つからsendは使わない        */
  221.     stat = 0x01 ;
  222.     while( ( inp( RS_IN ) & 0x03 ) != stat ) {
  223.         printf( "*\r" ) ;
  224.         if( kbhit() ) {
  225.             if( getch() == 0x1b ) sig_out() ;
  226.         }
  227.     }
  228.  
  229.     outp( RS_OUT , 0x20 ) ;        /* ずーと待つからsendは使わない        */
  230.     stat = 0x02 ;
  231.     while( ( inp( RS_IN ) & 0x03 ) != stat ) {
  232.         printf( "*\r" ) ;
  233.         if( kbhit() ) {
  234.             if( getch() == 0x1b ) sig_out() ;
  235.         }
  236.     }
  237.  
  238.     buffer[0] = 0xe4 ;            /* このへんの文字は何でも良い(^^;    */
  239.     if ( send( 1 , buffer ) ) {
  240.         if( mode == 2 ) return( -1 ) ;
  241.         timeout() ;
  242.     }
  243.     if( debug ) printf( "送信接続OK。\n" ) ;
  244.     return( 0 ) ;
  245.  
  246. }
  247.  
  248. /* 回線送信処理 */
  249. int send( int l , unsigned char *b )
  250. {
  251.     int i ;
  252.     long k ;
  253.     unsigned char stat0,stat1,data ;
  254.  
  255. /* 1バイトを8つに区切って1ビット毎に送信 */
  256.     for( i = 0 ; i < l ; i++ ) {
  257. /* ビット7送信 */
  258.         data = 0x00 ;
  259.         stat0 = 0x00 ;
  260.         if( (*b) & 0x80 ) {
  261.             data |= 0x02 ;
  262.             stat0 |= 0x01 ;
  263.         }
  264.         outp( RS_OUT , data ) ;
  265. /* ビット6送信 */
  266.         data = 0x20 ;
  267.         stat1 = 0x02 ;
  268.         if( (*b) & 0x40 ) {
  269.             data |= 0x02 ;
  270.             stat1 |= 0x01 ;
  271.         }
  272.         for( k = timeout1 ; ( inp( RS_IN ) & 0x03 ) != stat0 ; k-- ) {
  273.             if( !( k ) ) return( -1 ) ;
  274.         }
  275.         outp( RS_OUT , data ) ;
  276. /* ビット5送信 */
  277.         data = 0x00 ;
  278.         stat0 = 0x00 ;
  279.         if( (*b) & 0x20 ) {
  280.             data |= 0x02 ;
  281.             stat0 |= 0x01 ;
  282.         }
  283.         for( k = timeout1 ; ( inp( RS_IN ) & 0x03 ) != stat1 ; k-- ) {
  284.             if( !( k ) ) return( -1 ) ;
  285.         }
  286.         outp( RS_OUT , data ) ;
  287. /* ビット4送信 */
  288.         data = 0x20 ;
  289.         stat1 = 0x02 ;
  290.         if( (*b) & 0x10 ) {
  291.             data |= 0x02 ;
  292.             stat1 |= 0x01 ;
  293.         }
  294.         for( k = timeout1 ; ( inp( RS_IN ) & 0x03 ) != stat0 ; k-- ) {
  295.             if( !( k ) ) return( -1 ) ;
  296.         }
  297.         outp( RS_OUT , data ) ;
  298. /* ビット3送信 */
  299.         data = 0x00 ;
  300.         stat0 = 0x00 ;
  301.         if( (*b) & 0x08 ) {
  302.             data |= 0x02 ;
  303.             stat0 |= 0x01 ;
  304.         }
  305.         for( k = timeout1 ; ( inp( RS_IN ) & 0x03 ) != stat1 ; k-- ) {
  306.             if( !( k ) ) return( -1 ) ;
  307.         }
  308.         outp( RS_OUT , data ) ;
  309. /* ビット2送信 */
  310.         data = 0x20 ;
  311.         stat1 = 0x02 ;
  312.         if( (*b) & 0x04 ) {
  313.             data |= 0x02 ;
  314.             stat1 |= 0x01 ;
  315.         }
  316.         for( k = timeout1 ; ( inp( RS_IN ) & 0x03 ) != stat0 ; k-- ) {
  317.             if( !( k ) ) return( -1 ) ;
  318.         }
  319.         outp( RS_OUT , data ) ;
  320. /* ビット1送信 */
  321.         data = 0x00 ;
  322.         stat0 = 0x00 ;
  323.         if( (*b) & 0x02 ) {
  324.             data |= 0x02 ;
  325.             stat0 |= 0x01 ;
  326.         }
  327.         for( k = timeout1 ; ( inp( RS_IN ) & 0x03 ) != stat1 ; k-- ) {
  328.             if( !( k ) ) return( -1 ) ;
  329.         }
  330.         outp( RS_OUT , data ) ;
  331. /* ビット0送信 */
  332.         data = 0x20 ;
  333.         stat1 = 0x02 ;
  334.         if( (*b) & 0x01 ) {
  335.             data |= 0x02 ;
  336.             stat1 |= 0x01 ;
  337.         }
  338.         for( k = timeout1 ; ( inp( RS_IN ) & 0x03 ) != stat0 ; k-- ) {
  339.             if( !( k ) ) return( -1 ) ;
  340.         }
  341.         outp( RS_OUT , data ) ;
  342.         for( k = timeout1 ; ( inp( RS_IN ) & 0x03 ) != stat1 ; k-- ) {
  343.             if( !( k ) ) return( -1 ) ;
  344.         }
  345. /* 次のバイト処理へ */
  346.         b++ ;
  347.     }
  348.     return( 0 ) ;
  349.  
  350. }
  351.  
  352. /* 接続確認待ち(受信型)*/
  353. int connect_r()
  354. {
  355.     unsigned char stat ;
  356.  
  357. /* 接続確認 */
  358.     if( mode != 2 ) printf( "接続待ちです。\n" ) ;
  359.     stat = 0x01 ;
  360.     while( ( inp( RS_IN ) & 0x03 ) != stat ) {
  361.         printf( "*\r" ) ;
  362.         if( kbhit() ) {
  363.             if( getch() == 0x1b ) sig_out() ;
  364.         }
  365.     }
  366.     outp( RS_OUT , 0x02 ) ;        /* ずっと待ちたいからreceiveを使わない    */
  367.  
  368.     stat = 0x02 ;
  369.     while( ( inp( RS_IN ) & 0x03 ) != stat ) {
  370.         printf( "*\r" ) ;
  371.         if( kbhit() ) {
  372.             if( getch() == 0x1b ) sig_out() ;
  373.         }
  374.     }
  375.     outp( RS_OUT , 0x20 ) ;        /* ずっと待ちたいからreceiveを使わない    */
  376.  
  377.     if( receive( 1 , buffer ) ) {
  378.         if( mode == 2 ) return( -1 ) ;
  379.         timeout() ;
  380.     }
  381.     if( buffer[0] != 0xe4 ) {    /* このへんの文字は送信側に合わせる        */
  382.         printf( "接続確認処理でエラーが発生しました。\n" ) ;
  383.         if( mode == 2 ) return( -1 ) ;
  384.         rs_init() ;
  385.         exit( -1 ) ;
  386.     }
  387.     if( debug ) printf( "受信接続OK。\n" ) ;
  388.     return( 0 ) ;
  389.  
  390. }
  391.  
  392. /* 回線受信処理 */
  393. int receive( int l , unsigned char *b )
  394. {
  395.     int i ;
  396.     long k , t ;
  397.     unsigned char data ;
  398.  
  399. /* 1ビットを8つ受信で1バイト */
  400.     for( i = 0 ; i < l ; i++ ) {
  401.         *b = 0x00 ;
  402. /* ビット7受信 */
  403.         for( k = timeout1 ; inp( RS_IN ) & 0x02 ; k-- ) {
  404.             if( !( k ) ) return( -1 ) ;
  405.         }
  406.         for( t = 0l ; t < PALAWAIT ; t++ ) ; /* (;_;) */
  407.         data = 0x00 ;
  408.         if( inp( RS_IN ) & 0x01 ) {
  409.                 (*b) |= 0x80 ;
  410.             data |= 0x02 ;
  411.         }
  412.         outp( RS_OUT , data ) ;
  413. /* ビット6受信 */
  414.         for( k = timeout1 ; !(inp( RS_IN ) & 0x02) ; k-- ) {
  415.             if( !( k ) ) return( -1 ) ;
  416.         }
  417.         for( t = 0l ; t < PALAWAIT ; t++ ) ; /* (;_;) */
  418.         data = 0x20 ;
  419.         if( inp( RS_IN ) & 0x01 ) {
  420.                 (*b) |= 0x40 ;
  421.             data |= 0x02 ;
  422.         }
  423.         outp( RS_OUT , data ) ;
  424. /* ビット5受信 */
  425.         for( k = timeout1 ; inp( RS_IN ) & 0x02 ; k-- ) {
  426.             if( !( k ) ) return( -1 ) ;
  427.         }
  428.         for( t = 0l ; t < PALAWAIT ; t++ ) ; /* (;_;) */
  429.         data = 0x00 ;
  430.         if( inp( RS_IN ) & 0x01 ) {
  431.                 (*b) |= 0x20 ;
  432.             data |= 0x02 ;
  433.         }
  434.         outp( RS_OUT , data ) ;
  435. /* ビット4受信 */
  436.         for( k = timeout1 ; !(inp( RS_IN ) & 0x02) ; k-- ) {
  437.             if( !( k ) ) return( -1 ) ;
  438.         }
  439.         for( t = 0l ; t < PALAWAIT ; t++ ) ; /* (;_;) */
  440.         data = 0x20 ;
  441.         if( inp( RS_IN ) & 0x01 ) {
  442.                 (*b) |= 0x10 ;
  443.             data |= 0x02 ;
  444.         }
  445.         outp( RS_OUT , data ) ;
  446. /* ビット3受信 */
  447.         for( k = timeout1 ; inp( RS_IN ) & 0x02 ; k-- ) {
  448.             if( !( k ) ) return( -1 ) ;
  449.         }
  450.         for( t = 0l ; t < PALAWAIT ; t++ ) ; /* (;_;) */
  451.         data = 0x00 ;
  452.         if( inp( RS_IN ) & 0x01 ) {
  453.                 (*b) |= 0x08 ;
  454.             data |= 0x02 ;
  455.         }
  456.         outp( RS_OUT , data ) ;
  457. /* ビット2受信 */
  458.         for( k = timeout1 ; !(inp( RS_IN ) & 0x02) ; k-- ) {
  459.             if( !( k ) ) return( -1 ) ;
  460.         }
  461.         for( t = 0l ; t < PALAWAIT ; t++ ) ; /* (;_;) */
  462.         data = 0x20 ;
  463.         if( inp( RS_IN ) & 0x01 ) {
  464.                 (*b) |= 0x04 ;
  465.             data |= 0x02 ;
  466.         }
  467.         outp( RS_OUT , data ) ;
  468. /* ビット1受信 */
  469.         for( k = timeout1 ; inp( RS_IN ) & 0x02 ; k-- ) {
  470.             if( !( k ) ) return( -1 ) ;
  471.         }
  472.         for( t = 0l ; t < PALAWAIT ; t++ ) ; /* (;_;) */
  473.         data = 0x00 ;
  474.         if( inp( RS_IN ) & 0x01 ) {
  475.                 (*b) |= 0x02 ;
  476.             data |= 0x02 ;
  477.         }
  478.         outp( RS_OUT , data ) ;
  479. /* ビット0受信 */
  480.         for( k = timeout1 ; !(inp( RS_IN ) & 0x02) ; k-- ) {
  481.             if( !( k ) ) return( -1 ) ;
  482.         }
  483.         for( t = 0l ; t < PALAWAIT ; t++ ) ; /* (;_;) */
  484.         data = 0x20 ;
  485.         if( inp( RS_IN ) & 0x01 ) {
  486.                 (*b) |= 0x01 ;
  487.             data |= 0x02 ;
  488.         }
  489.         outp( RS_OUT , data ) ;
  490. /* 次のバイト処理へ */
  491.         b++ ;
  492.     }
  493.     return( 0 ) ;
  494.  
  495. }
  496.  
  497. /* 8251の初期化 */
  498. void rs_init()
  499. {
  500.     int i , j ;
  501.  
  502. /* 状態のリセット */
  503.     for( i = 0 ; i < 3 ; i++ ) {
  504.         outp( RS_OUT , 0x00 ) ;
  505.         for( j = 0 ; j < 3 ; j++ ) ; /* 念のため時間待ち */
  506.     }
  507.     outp( RS_OUT , 0x40) ;
  508.  
  509. /* モードセット */
  510.     outp( RS_OUT , 0x4e) ;
  511.     for( j = 0 ; j < 6 ; j++ ) ; /* 念のため時間待ち */
  512.  
  513. /* エラーリセット */
  514.     outp( RS_OUT , 0x37) ;
  515.     for( j = 0 ; j < 6 ; j++ ) ; /* 念のため時間待ち */
  516.  
  517. }
  518.  
  519. /* コピーライト表示 */
  520. void copyright()
  521. {
  522.     printf(
  523.         "IBM<=>98<=>FMR ファイル電送 for FMR Ver 1.01, Copyright (C) E.Suto , 1992-1993\n"
  524.         ) ;
  525. }
  526.  
  527. /* 使い方表示 */
  528. void usage()
  529. {
  530.     printf( "使用法                 : IPLINKF [options] [file-name]\n" ) ;
  531.     printf( "オプション -s            : ファイル送信(要送信ファイル名)\n" ) ;
  532.     printf( "      -r            : ファイル受信\n" ) ;
  533.     printf( "      -z            : サーバーモード\n" ) ;
  534.     printf( "      -cリモートコマンド  : サーバーにリモートコマンドを送信\n" ) ;
  535.     printf( "      -x[path_name] : -sでの格納先パス名\n" ) ;
  536.     printf( "          -i            : 詳細情報表示\n" ) ;
  537. }
  538.  
  539.